package com.js.config;

import com.js.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * author:JiangSong
 * Date:2023/5/21
 **/


public class LoginHandlerInterceptor implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException, IOException {
                //获取session域中的角色
                User admin = (User) request.getSession().getAttribute("admin");
                if (admin == null) {    //如果没有登录
                        request.setAttribute("msg","没有权限，请先登录");
                        System.out.println("can not to register!");
                        //请求转发
                        request.getRequestDispatcher("/admin/login").forward(request,response);
                        return false;
                } else {
                        return true;
                }
        }
}
